﻿
Imports Telerik.WinControls
Imports Telerik.WinControls.UI
Public Class RadFormFindandReplace
    Dim searchRow As GridViewSearchRowInfo = MainForm.RadGvidViewDralist.MasterView.TableSearchRow
    Dim searchCol As GridViewSearchRowInfo = MainForm.RadGvidViewDralist.MasterView.TableSearchRow
    Private Sub BtnFindAll_Click(sender As Object, e As EventArgs) Handles BtnFindAll.Click
        '第一种方法没有使用自带的API查询
        ''-----------------------------------------------------------------------------------------------------

        'Dim sCurrentColumnName As String

        ''InitializeComponent()
        'For Each rowInfo As GridViewRowInfo In MainForm.RadGvidViewDralist.Rows
        '    For Each cellInfo As GridViewCellInfo In rowInfo.Cells
        '        cellInfo.Style.ForeColor = Color.Black
        '    Next
        'Next
        'If ColumnList.Text = "WholeTable" Then
        '    For Each rowInfo As GridViewRowInfo In MainForm.RadGvidViewDralist.Rows
        '        For Each cellInfo As GridViewCellInfo In rowInfo.Cells
        '            'If (cellInfo.ColumnInfo.Name = "Title") OrElse (cellInfo.ColumnInfo.Name = "FirstName") OrElse (cellInfo.ColumnInfo.Name = "LastName") Then
        '            '    cellInfo.Value = "Test Value"
        '            'End If
        '            'cellInfo.Style.ForeColor = Color.Black
        '            If CheckwholeCell.Checked = True And CheckMatchCase.Checked = True Then
        '                If cellInfo.Value = TextFindWhat.Text Then
        '                    cellInfo.Style.ForeColor = Color.Red
        '                End If
        '            ElseIf CheckwholeCell.Checked = True And CheckMatchCase.Checked = False Then
        '                If UCase(cellInfo.Value) = UCase(TextFindWhat.Text) Then
        '                    cellInfo.Style.ForeColor = Color.Red
        '                End If

        '            ElseIf CheckwholeCell.Checked = False And CheckMatchCase.Checked = True Then

        '                If InStr(cellInfo.Value, TextFindWhat.Text) > 0 Then
        '                    cellInfo.Style.ForeColor = Color.Red
        '                End If

        '            ElseIf CheckwholeCell.Checked = False And CheckMatchCase.Checked = False Then
        '                If InStr(UCase(cellInfo.Value), UCase(TextFindWhat.Text)) > 0 Then
        '                    cellInfo.Style.ForeColor = Color.Red
        '                End If
        '            End If
        '        Next
        '    Next
        'ElseIf ColumnList.Text = "Column Only" Then
        '    Dim selectedColumnInfo As GridViewColumn
        '    Dim i As Integer
        '    i = 0
        '    selectedColumnInfo = MainForm.RadGvidViewDralist.CurrentCell.ColumnInfo
        '    sCurrentColumnName = selectedColumnInfo.Name
        '    For Each rowInfo As GridViewRowInfo In MainForm.RadGvidViewDralist.Rows
        '        If CheckwholeCell.Checked = True And CheckMatchCase.Checked = True Then

        '            If MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Value = TextFindWhat.Text Then
        '                MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Style.ForeColor = Color.Red
        '            End If
        '        ElseIf CheckwholeCell.Checked = False And CheckMatchCase.Checked = True Then

        '            If InStr(MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Value, TextFindWhat.Text) > 0 Then
        '                MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Style.ForeColor = Color.Red
        '            End If
        '        ElseIf CheckwholeCell.Checked = True And CheckMatchCase.Checked = False Then
        '            If UCase(MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Value) = UCase(TextFindWhat.Text) Then
        '                MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Style.ForeColor = Color.Red
        '            End If
        '        ElseIf CheckwholeCell.Checked = False And CheckMatchCase.Checked = False Then

        '            If InStr(UCase(MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Value), UCase(TextFindWhat.Text)) > 0 Then
        '                MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Style.ForeColor = Color.Red
        '            End If
        '        End If
        '        i = i + 1
        '    Next

        'End If
        '分割线
        '-----------------------------------------------------------------------------------------------------
        '------------------------------------------------------------------------------------------------------
        '使用Search Row 方法进行查询，给一个参数可以自动查找 searchRow.Search(TextFindWhat.Text）
        'RadGridCoumn.AllowSearching = False 默认是True，限制查找的列
        'searchRow.CaseSensitive = True 默认为Fale， 不区分大小写

        Dim RadGridCoumn As GridViewDataColumn
        searchRow.HighlightResults = True
        searchRow.AutomaticallySelectFirstResult = True

        If ColumnList.Text = "所有列" Then
            For Each RadGridCoumn In MainForm.RadGvidViewDralist.Columns
                RadGridCoumn.AllowSearching = True
            Next
            If CheckMatchCase.Checked = True Then
                searchRow.CaseSensitive = True  '区分大小写， 默认是不区分大小写
                searchRow.Search(TextFindWhat.Text)
            Else
                searchRow.CaseSensitive = False
                searchRow.Search(TextFindWhat.Text)
            End If
        Else
            If CheckMatchCase.Checked = True Then
                searchRow.CaseSensitive = True  '区分大小写， 默认是不区分大小写
                For Each RadGridCoumn In MainForm.RadGvidViewDralist.Columns
                    If RadGridCoumn.Name <> ColumnList.Text Then
                        RadGridCoumn.AllowSearching = False
                    Else
                        RadGridCoumn.AllowSearching = True
                    End If
                Next
                searchRow.Search(TextFindWhat.Text)
            ElseIf CheckMatchCase.Checked = False Then
                searchRow.CaseSensitive = False
                For Each RadGridCoumn In MainForm.RadGvidViewDralist.Columns
                    If RadGridCoumn.Name <> ColumnList.Text Then
                        RadGridCoumn.AllowSearching = False
                    Else
                        RadGridCoumn.AllowSearching = True
                    End If
                Next
                searchRow.Search(TextFindWhat.Text)
            End If
        End If

    End Sub

    Private Sub BtnReplaceAll_Click(sender As Object, e As EventArgs) Handles BtnReplaceAll.Click
        Dim sNewData As String
        sNewData = ""
        If ColumnList.Text = "所有列" Then
            For Each rowInfo As GridViewRowInfo In MainForm.RadGvidViewDralist.Rows
                For Each cellInfo As GridViewCellInfo In rowInfo.Cells
                    'If (cellInfo.ColumnInfo.Name = "Title") OrElse (cellInfo.ColumnInfo.Name = "FirstName") OrElse (cellInfo.ColumnInfo.Name = "LastName") Then
                    '    cellInfo.Value = "Test Value"
                    'End If
                    'If cellInfo.Style.ForeColor = Color.Red And CheckwholeCell.Checked Then


                    If CheckMatchCase.Checked = True Then

                        If InStr(cellInfo.Value, TextFindWhat.Text) > 0 Then
                            sNewData = Strings.Replace(cellInfo.Value, TextFindWhat.Text, TextReplaceWith.Text)
                            cellInfo.Value = sNewData
                        End If

                    ElseIf CheckMatchCase.Checked = False Then
                        If InStr(UCase(cellInfo.Value), UCase(TextFindWhat.Text)) > 0 Then
                            sNewData = Strings.Replace(cellInfo.Value, TextFindWhat.Text, TextReplaceWith.Text,,, 1)  '不区分大小写进行替换test替换，原来值为TESTVB，替换之后变成testVB
                            cellInfo.Value = sNewData
                        End If
                    End If

                Next
            Next
            'ElseIf ColumnList.Text = "Column Only" Then
        Else
            'Dim selectedColumnInfo As GridViewColumn
            Dim i As Integer
            Dim sCurrentColumnName As String

            i = 0
            ''selectedColumnInfo = MainForm.RadGvidViewDralist.CurrentCell.ColumnInfo
            'sCurrentColumnName = selectedColumnInfo.Name
            sCurrentColumnName = ColumnList.Text
            For Each rowInfo As GridViewRowInfo In MainForm.RadGvidViewDralist.Rows
                'If CheckwholeCell.Checked And MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Style.ForeColor = Color.Red Then

                If CheckMatchCase.Checked = True Then

                    If InStr(MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Value, TextFindWhat.Text) > 0 Then
                        sNewData = Strings.Replace(MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Value, TextFindWhat.Text, TextReplaceWith.Text)
                        MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Value = sNewData
                    End If
                ElseIf CheckMatchCase.Checked = False Then
                    If InStr(UCase(MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Value), UCase(TextFindWhat.Text)) > 0 Then
                        sNewData = Strings.Replace(MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Value, TextFindWhat.Text, TextReplaceWith.Text,, , 1)
                        MainForm.RadGvidViewDralist.Rows(i).Cells(sCurrentColumnName).Value = sNewData
                    End If
                End If
                i = i + 1
            Next
        End If
    End Sub

    Private Sub RadFormFindandReplace_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    'Private Sub RadButton1_Click(sender As Object, e As EventArgs) Handles RadButton1.Click

    '    Dim RadGridCoumn As GridViewDataColumn

    '    searchRow.HighlightResults = True
    '    searchRow.AutomaticallySelectFirstResult = True
    '    searchRow.CaseSensitive = True  '区分大小写， 默认是不区分大小写


    '    For Each RadGridCoumn In MainForm.RadGvidViewDralist.Columns
    '        If RadGridCoumn.Name <> "TEST1" Then
    '            RadGridCoumn.AllowSearching = False
    '        Else
    '            RadGridCoumn.AllowSearching = False
    '        End If

    '    Next
    '    searchRow.Search(TextFindWhat.Text)


    '    AddHandler searchRow.SearchProgressChanged, AddressOf mySearchResult

    'End Sub

    'Private Sub StartSearch()
    '    Dim searchRow As GridViewSearchRowInfo = Me.grdMyGridView.MasterView.TableSearchRow
    '    searchRow.SearchProgressChanged += AddressOf mySearchResult
    '    searchRow.Search(qrCode)
    '    qrCode = String.Empty
    'End Sub

    'Private Sub mySearchResult(ByVal sender As Object, ByVal e As SearchProgressChangedEventArgs)
    '    Dim iTotal As Integer
    '    searchRow.InitialSearchResultsTreshold = 0
    '    iTotal = searchRow.Cells.Count

    'End Sub
End Class
